#pragma once

#include<iostream>
using  std::cout;
using std::endl;
template <class T>
class ObjectPool
{
public:
	T* New()
	{
		T* obj = nullptr;
		if (_freelist)
		{
			void* next = *(void**)_freelist;
			obj = (T*)_freelist;
			_freelist = next;
		}
		else
		{
			if (_remainBytes < sizeof(T))
			{
				_remainBytes = 128 * 1024;
				_memory = (char*)malloc(_remainBytes);
				if (_memory == nullptr)
				{
					throw std::bad_alloc();
				
				
				}





			}
			obj = (T*)_memory;
			size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
			_memory += objSize;
			_remainBytes -= objSize;
		}
		new(obj)T;


		return obj;
	
	
	
	
	
	
	
	}

	void Delete(T* obj)
	{
	
		obj->~T();


		*(void**)obj = _freelist;
		_freelist = obj;
	
	
	
	}






private:
	char* _memory = nullptr;
	size_t _remainBytes = 0;
	void* _freelist = nullptr;


};




